package 第3节函数;
//二分搜索 递归版本
public class RecursionDemo03 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        int key = 11;
        System.out.println(binarySearch(arr,0, arr.length - 1, key));
    }
    public static int binarySearch(int[] arr, int L, int R, int key) {
        if (R < L) {
            return -1;
        }
        int M = (L + R) / 2;
        if (arr[M] < key) {
            return binarySearch(arr,M + 1, R, key);
        } else if (key < arr[M]) {
            return binarySearch(arr,L, M - 1, key);
        } else {
            return M;
        }
    }
}
